var NS={
  XHTML: "http://www.w3.org/1999/xhtml"
}

var expanded;
var created;
function showHideComment(isAdmin){
  if(!created){
    createComment(isAdmin);
    created=true;
    expanded=true;
    return;
  }
  if(expanded){
    hideComment();
    expanded=false;
    return;
  }
  showComment();
  expanded=true;
}

function hideComment(){
  document.getElementById("comment").setAttribute("style","display:none");  
}

function showComment(){
  document.getElementById("comment").setAttribute("style","");
}

function decode(str){
  return str.replace(/\\n/,"\n").replace(/\\r/,"\r").replace(/\\c/,",").replace(/\\\\/,"\\");
}

var FORM_INPUT=1;
var FORM_TEXTAREA=2;
var FORM_PASSWORD=3;
var FORM_SUBMIT=4;
var FORM_HIDDEN=5;

function appendFormItem(form,title,name,type,value){
  if(!type)type=FORM_INPUT;
  var label;
  if(type!=FORM_SUBMIT&&type!=FORM_HIDDEN){
    label=document.createElementNS(NS.XHTML,"label");
    label.appendChild(document.createTextNode(title));
  }
  var input=document.createElementNS(NS.XHTML,type==FORM_TEXTAREA?"textarea":"input");
  if(name){
    input.setAttribute("name",name);
    input.setAttribute("id",name);
  }
  if(type==FORM_PASSWORD)input.setAttribute("type","password");
  if(type==FORM_SUBMIT){
    input.setAttribute("type","submit");
    input.setAttribute("value",title);
    form.appendChild(input);
    return;
  }
  if(type==FORM_HIDDEN){
    input.setAttribute("type","hidden");
    input.setAttribute("value",value);
    form.appendChild(input);
    return;
  }
  label.appendChild(input);
  form.appendChild(label);
}

function createComment(isAdmin){
  var container=document.createElementNS(NS.XHTML,"div");
  container.setAttribute("id","comment");
  var baseuri;
  var index=location.href.indexOf("?");
  if(index==-1){
    baseuri=location.href;
  }else{
    baseuri=location.href.substring(0,index);
  }
  /*Ajaxを使ったコメントの表示*/
  var req=new XMLHttpRequest();
  req.open("GET", baseuri+"?mode=commentshow", false);//XXX: PATH_INFOを使わない場合は未対応
  req.send(null);
  if(req.status==200){
    var arr=req.responseText.split("\n");
    var dl=document.createElementNS(NS.XHTML,"dl");
    var i=0;
    for(var l in arr){
      if(arr[l].indexOf(",")==-1)continue;
      var dt=document.createElementNS(NS.XHTML,"dt");
      var dd=document.createElementNS(NS.XHTML,"dd");
      var arr2=arr[l].split(",");
      var name=document.createElementNS(NS.XHTML,"span");
      name.setAttribute("class","name");
      name.appendChild(document.createTextNode(decode(arr2[0])));
      dt.appendChild(name);
      if(arr2[3]){
        var url=document.createElementNS(NS.XHTML,"span");
        url.setAttribute("class","url");
        var a=document.createElementNS(NS.XHTML,"a");
        a.setAttribute("href",decode(arr2[3]));
        a.appendChild(document.createTextNode("[url]"));
        url.appendChild(a);
        dt.appendChild(url);
      }
      if(arr2[4]){
        var mail=document.createElementNS(NS.XHTML,"span");
        mail.setAttribute("class","mail");
        var a=document.createElementNS(NS.XHTML,"a");
        a.setAttribute("href","mailto:"+decode(arr2[4]));
        a.appendChild(document.createTextNode("[mail]"));
        mail.appendChild(a);
        dt.appendChild(mail);
      }
      var p=document.createElementNS(NS.XHTML,"p");
      p.appendChild(document.createTextNode(decode(arr2[1])));
      dd.appendChild(p);
      var delform=document.createElementNS(NS.XHTML,"form");
      delform.setAttribute("action",baseuri);
      delform.setAttribute("method","POST");
      appendFormItem(delform,null,"mode",FORM_HIDDEN,"wiki:commentdelete");
      appendFormItem(delform,null,"no",FORM_HIDDEN,i.toString());
      if(!isAdmin)
        appendFormItem(delform,"削除パス","del");
      appendFormItem(delform,"削除",null,FORM_SUBMIT);
      dd.appendChild(delform);
      dl.appendChild(dt);
      dl.appendChild(dd);
      i++;
    }
    container.appendChild(dl);
  }else{
    var p=document.createElementNS(NS.XHTML,"p");
    p.appendChild(document.createTextNode("何故かコメントの取得がうまくできないようです(汗"));
    container.appendChild(p);
  }
  /*書き込みフォームの作成*/
  var writeform=document.createElementNS(NS.XHTML,"form");
  writeform.setAttribute("action",baseuri);
  writeform.setAttribute("method","POST");
  if(!isAdmin){
    appendFormItem(writeform,"名前(トリップ必須)","name");
    appendFormItem(writeform,"メール","mail");
    appendFormItem(writeform,"URL","url");
    writeform.appendChild(document.createElementNS(NS.XHTML,"br"));
  }
  appendFormItem(writeform,"本文","message",FORM_TEXTAREA);
  writeform.appendChild(document.createElementNS(NS.XHTML,"br"));
  if(!isAdmin)
    appendFormItem(writeform,"削除パス","del",FORM_PASSWORD);
  appendFormItem(writeform,null,"mode",FORM_HIDDEN,"wiki:commentwrite");
  var title;
  var index2=location.href.indexOf("index.cgi/");
  if(index2!=-1){
    index2+="index.cgi/".length;
    title=unescape(location.href.substring(index2));
  }
  appendFormItem(writeform,null,"title",FORM_HIDDEN,title);//for !PATH_INFO
  container.appendChild(writeform);
  appendFormItem(writeform,"送信",null,FORM_SUBMIT);
  document.getElementById("main").appendChild(container);
}
